package com.umfwechat.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MessageDigestUtil {
	public static String digest(String password) throws NoSuchAlgorithmException {
		MessageDigest md = MessageDigest.getInstance("SHA-1");
		// 对拼接后的字符串进行 sha1 加密
		byte[] digest = md.digest(password.getBytes());
		String str = byteToStr1(digest);
		return str;

	}
	private static String byteToStr1(byte[] byteArray) {  
        String strDigest = "";  
        for (int i = 0; i < byteArray.length; i++) {  
            strDigest += byteToHexStr1(byteArray[i]);  
        }  
        return strDigest;  
	 }  
	/** 
     * 将字节转换为十六进制字符串 
     *  
     * @param mByte 
     * @return 
     */  
	private static String byteToHexStr1(byte mByte) {  
	    char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };  
	    char[] tempArr = new char[2];  
	    tempArr[0] = Digit[(mByte >>> 4) & 0X0F];  
	    tempArr[1] = Digit[mByte & 0X0F];  
	
	    String s = new String(tempArr);  
	    return s;  
	}  
	public static void main(String[] args) throws NoSuchAlgorithmException {
		MessageDigest md = MessageDigest.getInstance("SHA-1");
		// 对拼接后的字符串进行 sha1 加密
		byte[] digest = md.digest("admin123".getBytes());
		String str = byteToStr1(digest);
		System.out.println(str);
		System.out.println(str.length());
	}
}
